<?php

namespace App\Http\Controllers\Admin;

use App\model\AddShop;
use App\Model\ShopGkvModel;
use App\Model\ShopGoodPicModel;
use App\Sku;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Validator;
use App\Model\SkuModel;
use App\Model\ShopGoodModel;

use App\Model\ShopAttrTypeModel;
use App\Model\ShopAttrKeyModel;
use App\Model\ShopAttrValModel;
use App\Model\ShopAttrTkvModel;



class SkuController extends Controller
{
    public function index(Request $request){
		$shop_id = $request->id;
		$gname = ShopGoodModel::where('id',$shop_id)->value('name');
		return view('admin.sku.index')->with('id',$shop_id)->with('gname',$gname)->with('request',$request);
	}
	public function sku_lists(Request $request){

		$shop_id = $request->id;
		$limit = $request->get('limit',10);
		$results = SkuModel::where('gid',$shop_id)->orderBy('id','desc')->paginate($limit);
		foreach($results as $k=>$v){
			$v['attr'] = $v->property;
			$v['picname'] = "http://".$_SERVER['HTTP_HOST'].$v['picname'];
		}
		return $this->layuiData($results);
	}

	public function add(Request $request){
    	$id = request()->id;
    	//查询规格ID
		$attrtype_id = ShopGoodModel::where('id',$id)->value('attrtype_id');
		//查询该规格的属性值
		$yxz_specification = ShopAttrTypeModel::with('attrkey')->find($attrtype_id);
		$yxzlist = array();
		//追加数组
		foreach ($yxz_specification->attrkey as $specifications) {
			//如果已将存在就不往里面添加
			//已选中的规格(颜色....)
			if(!in_array($specifications,$yxzlist)){
				array_push($yxzlist,$specifications);
			}
		}
		//已选中的规格id组合
		$kids = "";
		$kidss = array();
		foreach($yxzlist as $k=>$v){
			$kids .= $v->pivot->Kid.",";
			array_push($kidss,$v->pivot->Kid);
			//所有属性
			$values = ShopAttrValModel::where('attr_key_id',$v->pivot->Kid)->get();
			foreach($values as $vk=>$vv){
				//属性是否已选中
				$is_xz = ShopAttrTkvModel::where('tid',$v->pivot->tid)->where('kid',$v->pivot->Kid)->where('vid',$vv->id)->first();
				if($is_xz){
					//已选中
					$values[$vk]['is_xz'] = 1;
				}else{
					//未选中
					$values[$vk]['is_xz'] = 2;
				}
			}
			$yxzlist[$k]['info'] = $values;
		}
		$gname = ShopGoodModel::where('id',$id)->value('name');
		//加载
		return view('admin.sku.add')->with(['yxzlist'=>$yxzlist,'shop_id'=>$id,'id'=>$attrtype_id,'gname'=>$gname,'request'=>$request]);
	}
	//添加商品_规格价格
	public function sku_add(Request $request){
		$all = $request->all();
		//商品ID
		$shop_id = $request->get('shop_id',null);
		if(empty(request()->store_id)){
			return ['code'=>0,'msg'=>"请输入货源ID"];
		}
		//库存
//		$sum = $request->get('sum',null);
		//销量
//		$sale_num = $request->get('sale_num',null);
		$sku = new SkuModel();

		$attr = "";
		$attr_name = "";
		foreach($all as $k=>$v){
			if(is_int($k)){
				$attr .= $v.",";
				$cy = explode('-',$v);
				$attr_name .="'". ShopAttrValModel::where('id',$cy[1])->value('vname')."' " ;
			}
		}
		if(empty($attr)){
			return ['code'=>0,'msg'=>'请选择规格属性'];
		}
		$sku->property_id = $attr;
		$sku->attr = $attr_name;
		$sku->gid = $shop_id;
		//货源商品
		$store_list = AddShop::where('id',request()->store_id)->first();
		if($store_list){
			$sku->cost_price = $store_list['cost'];
			$sku->sell_price = $store_list['new_sell_price'];
			$sku->sum = $store_list['num'];
		}
		if(request()->picname){
			$sku->picname = "/upload/shop/".request()->picname;
		}
		$has = SkuModel::where('gid',$shop_id)->where('property_id',$attr)->first();
		if(!empty($has)){
			return ['code'=>0,'msg'=>'该规格组合已存在'];
		}

		foreach($all as $k=>$v){
			if(is_int($k)){
				$str = explode("-",$v);

				if(empty($str[0])){
					return ['code'=>0,'msg'=>'请选择规格属性'];
				}
				try {
					$gkv = new ShopGkvModel();
					$gkv->gid = $shop_id;
					$gkv->kid = $str[0];
					$gkv->vid = $str[1];
					$hass = ShopGkvModel::where('gid',$shop_id)->where('kid',$str[0])->where('vid',$str[1])->first();
					if(!empty($hass)){
						$gkv->save();
					}else{
						ShopGkvModel::where('gid',$shop_id)->where('kid',$str[0])->where('vid',$str[1])->increment('num',1);
					}
				} catch (\Exception $exception) {
					return ['code'=>0,'msg'=>$exception->getMessage()];
				}
			}
		}
		try {
//			$sku->sum = $sum;
//			$sku->sale_num = $sale_num;
//			$sku->cost_price = request()->cost_price;
			$sku->store_id = request()->store_id;
			$sku->save();
			return ['code'=>1,'msg'=>'操作成功'];
		} catch (\Exception $exception) {
			return ['code'=>0,'msg'=>$exception->getMessage()];
		}
	}

	//编辑规格信息
	public function sku_edit(Request $request){
		$id = request()->id;
    	$list = SkuModel::where('id',$id)->first();
    	if(!$list){
    		return ['code'=>0,'msg'=>'该规格不存在'];
		}
    	$gname = ShopGoodModel::where('id',$list['gid'])->value('name');
    	return view('admin.sku.edit',compact('id','list','request','gname'));
	}

	//编辑
	public function sku_update(){
//    	if(!request()->cost_price){
//    		return ['code'=>0,'msg'=>'请输入成本价'];
//		}
//    	if(!request()->sell_price){
//    		return ['code'=>0,'msg'=>'请输入销售价'];
//		}
    	if(!request()->store_id){
    		return ['code'=>0,'msg'=>'请输入货源ID'];
		}
    	DB::beginTransaction();
    	try{
    		if(request()->store_id){
				$store_list = AddShop::where('id',request()->store_id)->first();
				if($store_list){
					$cost_price = $store_list['cost'];
					$sell_price = $store_list['new_sell_price'];
					$sum = $store_list['num'];
				}
			}


			SkuModel::where('id',request()->id)->update([
//				'cost_price' => request()->cost_price,
//				'sell_price' => request()->sell_price,
//				'sum' => request()->sum,
//				'sale_num' => request()->sale_num,
				'picname' => request()->picname,
				'store_id' => request()->store_id,
				'cost_price' => $cost_price,
				'sell_price' => $sell_price,
				'sum' => $sum,
			]);
    		DB::commit();
    		return ['code'=>1,'msg'=>'编辑成功'];
		}catch(\Exception $e){
    		DB::rollBack();
    		return ['code'=>0,'msg'=>'编辑失败'];
		}
	}

	//删除
	public function sku_del(){
		if(!request()->id){
			return ['code'=>0,'msg'=>'非法入侵'];
		}
		DB::beginTransaction();
		try{
			SkuModel::where('id',request()->id)->delete();
			DB::commit();
			return ['code'=>1,'msg'=>'删除成功'];
		}catch(Exception $exception){
			DB::rollBack();
			return ['code'=>0,'msg'=>'删除失败'];
		}
	}

	//编辑图片
	public function edit_skupic(){
    	$id = request()->id;
    	$list = SkuModel::where('id',$id)->first();
		$list['oldname'] = $list['picname'];
		$list['picname'] = "http://".$_SERVER['HTTP_HOST'].$list['picname'];
    	return view('admin.sku.edit_skupic',compact('list','id'));
	}

	//编辑图片
	public function update_skupic(){
		$id = request()->id;
		DB::beginTransaction();
		try{
			SkuModel::where('id',$id)->update([
				'picname' => "/upload/shop/".request()->picname,
				'oss_pic' => request()->picname,
			]);
			DB::commit();
		}catch(Exception $e){
			DB::rollBack();
		}
		return ['code'=>1,'msg'=>'上传成功'];
	}
}
